import { defineStore } from "pinia";
//引入本地存储
import {
  SET_TOKEN,
  GET_TOKEN,
  REMOVE_TOKEN,
  SET_INFO,
  GET_INFO,
  REMOVE_INFO,
} from "@/utils/token";
import {
  GET_TELD_TOKEN,
  REMOVE_TELD_TOKEN,
  SET_TELD_TOKEN,
  GET_OPENID,
  REMOVE_OPENID,
  SET_OPENID,
} from "../utils/token";
//创建用户小仓库
const useUserStore = defineStore("User", {
  state: () => {
    return {
      teldToken: GET_TELD_TOKEN() || "",
      token: GET_TOKEN() || "",
      info: GET_INFO() || "{}",
      activeTab: 0, // 默认选中的索引
      code: "", // 微信 登录 code
      openid: GET_OPENID(), // 微信 openid
      roleId: "", // 角色id
      discount: {
        priceDiscount: 1,
        serviceDiscount: 1,
      },
      userRoleInfo: {
        roleId: 3, // 默认普通用户
        priceDiscount: 1, // 默认价格折扣
        serviceDiscount: 1, // 默认服务折扣
      },
    };
  },
  actions: {
    //获取用户信息
    updateUserInfo(info) {
      SET_INFO(JSON.stringify(info));
    },
    //获取token
    updateToken(token) {
      SET_TOKEN(token);
    },
    updateTeldToken(token) {
      this.teldToken = token;
      SET_TELD_TOKEN(token);
    },
    updateOpenId(id) {
      this.openid = id;
      SET_OPENID(id);
    },
    //退出登录
    userLogout() {
      this.token = "";
      this.teldToken = "";
      this.info = {};
      REMOVE_TOKEN();
      REMOVE_TELD_TOKEN();
      REMOVE_INFO();
      REMOVE_OPENID();
    },
    //设置active的值
    setActive(active) {
      this.activeTab = active;
    },
  },
});

//暴露用户小仓库
export default useUserStore;
